package cn.line5.seam.student;

import java.util.List;

import cn.line5.seam.annotations.Name;
import cn.line5.seam.db.PageResult;
import cn.line5.seam.db.Query;
import cn.line5.seam.db.QueryFactory;

@Name("studentDao")
/**
 * <h1>LSEAM框架示例数据库实现类</h1>
 * <br/>
 * 
 * @author Liu,yandong
 */
public class StudentDaoImpl implements StudentDao {
	public List<StudentVo> getList() {
		Query query = QueryFactory
				.createQuery("select user_id as userId, login_name as loginName, login_pass as loginPass, user_name as userName, user_photo as userPhoto from t_user");
		List<StudentVo> l = query.list(StudentVo.class);
		return l;
	}

	public PageResult<StudentVo> getPageList(String currentPage) {
		Integer page = Integer.parseInt(currentPage == null
				|| currentPage.length() <= 0 ? "1" : currentPage);
		Query query = QueryFactory
				.createQuery(
						"select user_id as userId, login_name as loginName, login_pass as loginPass, user_name as userName, user_photo as userPhoto from t_user")
				.setCurrentPage(
						page > 0 ? page : 1).setPageRows(3);
		PageResult<StudentVo> pr = query.pageList(StudentVo.class);
		return pr;
	}

	public StudentVo getStudent(String id) {
		Query query = QueryFactory
				.createQuery(
						"select user_id as userId, login_name as loginName, login_pass as loginPass, user_name as userName, user_photo as userPhoto from t_user where user_id=?")
				.setParameter(1, id);
		return query.single(StudentVo.class);
	}

	public StudentVo loginStudent(String loginAccount,String loginPassword){
		String sql = " select "
					+" student_id as studentId, "								//'主键',
					+" student_name as studentName, "							//'学生姓名',
					+" student_login_account as studentLoginAccount, "			//'登陆账号'
					+" student_login_password as studentLoginPassword, "		//'登陆密码'
					+" student_age as studentAge, " 							//'学生年龄'
					+" student_parent_name as studentParentName, "				//'家长姓名'
					+" student_parent_call as studentParentCall, "				//'家长电话'
					+" student_parent_mail as studentParentMail, "				//'家长邮箱'
					+" student_home_address as studentHomeAddress, "			//'家庭住址'
					+" student_introduction as studentIntroduction, "			//'学生情况说明'
					+" student_confirmation as studentConfirmation, "			//'验证方法'
					+" student_registration_date as studentRegistrationDate, "	//'注册时间'
					+" student_test_time as studentTestTime, "					//'预约测试时间'
					+" student_last_login_time as studentLastLoginTime, "		//'最后登录时间'
					+" student_state as studentState "							//'学生状态'
					+" from miko_student "
					+" where 1=1 ";
		if(loginAccount != null && !loginAccount.equals("")){
				sql+=" and student_login_account='"+loginAccount+"' ";
		}
		if(loginPassword != null && !loginPassword.equals("")){
				sql+=" and student_login_password='"+loginPassword+"' ";
		}
		
		Query query = QueryFactory.createQuery(sql);
		
		return query.single(StudentVo.class);
	}
	
	public StudentVo getStudentVo(StudentVo studentVo) {
		Query query = QueryFactory.createQuery(
				 			 " select "
							+" student_id as studentId, "								//'主键',
							+" student_name as studentName, "							//'学生姓名',
							+" student_login_account as studentLoginAccount, "			//'登陆账号'
							+" student_login_password as studentLoginPassword, "		//'登陆密码'
							+" student_age as studentAge, " 							//'学生年龄'
							+" student_parent_name as studentParentName, "				//'家长姓名'
							+" student_parent_call as studentParentCall, "				//'家长电话'
							+" student_parent_mail as studentParentMail, "				//'家长邮箱'
							+" student_home_address as studentHomeAddress, "			//'家庭住址'
							+" student_introduction as studentIntroduction, "			//'学生情况说明'
							+" student_confirmation as studentConfirmation, "			//'验证方法'
							+" student_registration_date as studentRegistrationDate, "	//'注册时间'
							+" student_test_time as studentTestTime, "					//'预约测试时间'
							+" student_last_login_time as studentLastLoginTime, "		//'最后登录时间'
							+" student_state as studentState "							//'学生状态'
							+" from miko_student "
							+" where student_name=? "
							+" and student_parent_name=? "
							+" and student_parent_call=? " )
							.setParameter(1, studentVo.getStudentName())
							.setParameter(2, studentVo.getStudentParentName())
							.setParameter(3, studentVo.getStudentParentCall());
		
		return query.single(StudentVo.class);
	}

	public boolean insertStudent(StudentVo vo) {
		Query query = QueryFactory.createExecute("insert into miko_student "
					+" values( "
					+" :studentId, "					//'主键',
					+" :studentName, "					//'学生姓名',
					+" :studentLoginAccount, "			//'登陆账号'
					+" :studentLoginPassword, "			//'登陆密码'
					+" :studentAge, " 					//'学生年龄'
					+" :studentParentName, "			//'家长姓名'
					+" :studentParentCall, "			//'家长电话'
					+" :studentParentMail, "			//'家长邮箱'
					+" :studentHomeAddress, "			//'家庭住址'
					+" :studentIntroduction, "			//'学生情况说明'
					+" :studentConfirmation, "			//'验证方法'
					+" :studentRegistrationDate, "		//'注册时间'
					+" :studentTestTime, "				//'预约测试时间'
					+" :studentLastLoginTime, "			//'最后登录时间'
					+" :studentState) ",				//'学生状态'
						vo);		
		
		return query.execute() > 0;
	}
	
	public boolean modifyStudent(StudentVo vo) {
		Query query = QueryFactory
				.createExecute(
						"update t_user set login_name=:loginName, login_pass=:loginPass, user_name=:userName, user_photo=:userPhoto where user_id=:userId",
						vo);
		return query.execute() > 0;
	}

	public boolean deleteStudent(String id) {
		Query query = QueryFactory.createExecute(
				"delete from t_user where user_id=?").setParameter(1, id);
		return query.execute() > 0;
	}
}
